import 'package:flutter/material.dart';

enum LoadState {
  SUCCESS, //成功
  ERROR, //失败
  LOADING, //价值啊中
  EMPTY //空
}

class LoadStateView extends StatefulWidget {
  final LoadState loadState; //页面状态
  final Widget successWidget; //成功界面
  final VoidCallback erroRetry; //错误事件处理
  LoadStateView({
    Key key,
    this.loadState = LoadState.LOADING,
    this.successWidget,
    this.erroRetry,
  }) : super(key: key);

  @override
  _LoadStateViewState createState() => _LoadStateViewState();
}

class _LoadStateViewState extends State<LoadStateView> {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      height: double.infinity,
      child: _contentView,
    );
  }

  Widget get _contentView {
    switch (widget.loadState) {
      case LoadState.SUCCESS:
        return widget.successWidget;
      case LoadState.EMPTY:
        return _emptyView;
      case LoadState.ERROR:
        return _errorView;
      case LoadState.LOADING:
        return _loadingView;
      default:
        return null;
    }
  }

  ///加载中界面
  Widget get _loadingView {
    return Container(
      width: double.infinity,
      height: double.infinity,
      alignment: Alignment.center,
      decoration: BoxDecoration(color: Colors.transparent),
      child: Container(
        height: 100,
        width: 100,
        padding: EdgeInsets.all(10),
        decoration: BoxDecoration(
          color: Color(0x88000000),
          borderRadius: BorderRadius.circular(10),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            CircularProgressIndicator(),
            SizedBox(height: 14),
            Text(
              "加载中...",
              style: TextStyle(color: Colors.white, fontSize: 18),
            ),
          ],
        ),
      ),
    );
  }

  ///错误界面
  Widget get _errorView {
    return Container(
      width: double.infinity,
      height: double.infinity,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(
            "images/load_error_view.png",
            height: 200,
            width: 200,
          ),
          SizedBox(
            height: 20,
          ),
          Text(
            "加载失败，请重试！",
            style: TextStyle(
              color: Colors.black,
              fontSize: 18,
            ),
          ),
          SizedBox(height: 20),
          Container(
            width: 170,
            height: 40,
            child: RaisedButton(
              onPressed: widget.erroRetry,
              color: Colors.blue,
              shape: StadiumBorder(),
              child: Text(
                "重新加载",
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 18,
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  ///空界面
  Widget get _emptyView {
    return Container(
      width: double.infinity,
      height: double.infinity,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Image.asset(
            'images/ic_empty.png',
            width: 200,
            height: 200,
          ),
          SizedBox(height: 10),
          Text(
            "暂无数据",
            style: TextStyle(
              color: Colors.black,
              fontSize: 20,
            ),
          ),
        ],
      ),
    );
  }
}
